package com.konke.smartpad_huahao.domain;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.konke.sdslibrary.model.UniformDeviceType;
import com.zsj.comminlibrary.util.GsonBuild;
import com.zsj.comminlibrary.util.SpManager;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AppCache {

    private static String mCacheDir;

    public static void initialize(Context context) {
        String path = context.getExternalCacheDir().getAbsoluteFile() + "/smartpad";
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        mCacheDir = path;
    }

    public static void putSuperCode(String code) {
        SpManager spManager = SpManager.getInstance();
        spManager.putString("superCode", code);
    }

    public static String getSuperCode() {
        SpManager spManager = SpManager.getInstance();
        return spManager.getString("superCode");
    }

    /**
     * [{"ccuId":"00162","rooms":[1,2,3]},{"ccuId":"0042","rooms":[1,2,3]}]
     * 若返回null 则说明没有限制全部显示
     *
     * @param ccuId
     * @return
     */
    public static List<Integer> getHideRoomIds(String ccuId) {
        SpManager instance = SpManager.getInstance();
        String rooms = instance.getString("rooms");
        if (rooms == null || TextUtils.isEmpty(rooms)) {
            return null;
        }
        Map<String, List<Integer>> map = new HashMap<>();
        Gson gson = GsonBuild.buildGson();
        map = gson.fromJson(rooms, map.getClass());
        if (map.keySet().contains(ccuId)) {
            List<Integer> integers = map.get(ccuId);
            return integers;
        }
        return null;
    }

    public static void putHideRooms(String ccuId, List<Integer> roomIds) {
        SpManager instance = SpManager.getInstance();
        String rooms = instance.getString("rooms");
        Map<String, List<Integer>> map = new HashMap<>();
        Gson gson = GsonBuild.buildGson();
        if (rooms != null && !TextUtils.isEmpty(rooms)) {
            map = gson.fromJson(rooms, map.getClass());
        }
        map.put(ccuId, roomIds);
        instance.putString("rooms", gson.toJson(map));
    }

    public static List<Integer> getHideFloorIds(String ccuId) {
        SpManager instance = SpManager.getInstance();
        String rooms = instance.getString("floors");
        if (rooms == null || TextUtils.isEmpty(rooms)) {
            return null;
        }
        Map<String, List<Integer>> map = new HashMap<>();
        Gson gson = GsonBuild.buildGson();
        map = gson.fromJson(rooms, map.getClass());
        if (map.keySet().contains(ccuId)) {
            List<Integer> integers = map.get(ccuId);
            return integers;
        }
        return null;
    }

    public static void putHideFloors(String ccuId, List<Integer> roomIds) {
        SpManager instance = SpManager.getInstance();
        String rooms = instance.getString("floors");
        Map<String, List<Integer>> map = new HashMap<>();
        Gson gson = GsonBuild.buildGson();
        if (rooms != null && !TextUtils.isEmpty(rooms)) {
            map = gson.fromJson(rooms, map.getClass());
        }
        map.put(ccuId, roomIds);
        instance.putString("floors", gson.toJson(map));
    }


    public static List<SimpleDevice> getHideSimpleDevices(String ccuId) {
        String devices = SpManager.getInstance().getString("devices");
        if (devices == null || TextUtils.isEmpty(devices)) {
            return null;
        }
        Map<String, List<String>> map = new HashMap<>();
        Gson gson = GsonBuild.buildGson();
        map = gson.fromJson(devices, map.getClass());
        if (map.keySet().contains(ccuId)) {
            List<String> stringSimpleDevices = map.get(ccuId);
            if (stringSimpleDevices != null && stringSimpleDevices.size() > 0) {
                List<SimpleDevice> list = new ArrayList<>();
                for (String s : stringSimpleDevices) {
                    String[] s1 = s.split("--");
                    if (s1 == null || s1.length != 2) {
                        continue;
                    }
                    list.add(new SimpleDevice(s1[0], UniformDeviceType.valueOf(s1[1])));
                }
                return list;
            }
        }
        return null;
    }

    public static void putHideSimpleDevices(String ccuId, List<SimpleDevice> simpleDevices) {
        String devices = SpManager.getInstance().getString("devices");
        Map<String, List<String>> map = new HashMap<>();
        Gson gson = GsonBuild.buildGson();
        if (devices != null && !TextUtils.isEmpty(devices)) {
            map = gson.fromJson(devices, map.getClass());
        }
        List<String> list = null;
        if (simpleDevices != null && simpleDevices.size() > 0) {
            list = new ArrayList<>();
            for (SimpleDevice device : simpleDevices) {
                list.add(device.getDevId() + "--" + device.getDeviceType().name());
            }
        }
        map.put(ccuId, list);
        SpManager.getInstance().putString("devices", gson.toJson(map));
    }


    public static List<Integer> getHideScenes(String ccuId) {
        SpManager instance = SpManager.getInstance();
        String scenes = instance.getString("scenes");
        if (scenes == null || TextUtils.isEmpty(scenes)) {
            return null;
        }
        Map<String, List<Integer>> map = new HashMap<>();
        Gson gson = GsonBuild.buildGson();
        map = gson.fromJson(scenes, map.getClass());
        if (map.keySet().contains(ccuId)) {
            List<Integer> integers = map.get(ccuId);
            if (integers != null && integers.size() > 0) {
                List<Integer> ins = new ArrayList<>();
                for (Object o : integers) {
                    if (o instanceof Number) {
                        int i = ((Double) o).intValue();
                        ins.add(i);
                    }
                }
                return ins;
            }
            return integers;
        }
        return null;
    }

    public static void putHideScenes(String ccuId, List<Integer> scenes) {
        String devices = SpManager.getInstance().getString("scenes");
        Map<String, List<Integer>> map = new HashMap<>();
        Gson gson = GsonBuild.buildGson();
        if (devices != null && !TextUtils.isEmpty(devices)) {
            map = gson.fromJson(devices, map.getClass());
        }
        map.put(ccuId, scenes);
        SpManager.getInstance().putString("scenes", gson.toJson(map));
    }


    public static List<Integer> getSortRoomIds(String ccuId) {
        SpManager instance = SpManager.getInstance();
        String rooms = instance.getString("sortrooms");
        if (rooms == null || TextUtils.isEmpty(rooms)) {
            return null;
        }
        Map<String, List<Integer>> map = new HashMap<>();
        Gson gson = GsonBuild.buildGson();
        map = gson.fromJson(rooms, map.getClass());
        if (map.keySet().contains(ccuId)) {
            List<Integer> integers = map.get(ccuId);
            if (integers != null && integers.size() > 0) {
                List<Integer> ins = new ArrayList<>();
                for (Object o : integers) {
                    if (o instanceof Number) {
                        int i = ((Double) o).intValue();
                        ins.add(i);
                    }
                }
                return ins;
            }
            return integers;
        }
        return null;
    }

    public static void putSortRooms(String ccuId, List<Integer> roomIds) {
        SpManager instance = SpManager.getInstance();
        String rooms = instance.getString("sortrooms");
        Map<String, List<Integer>> map = new HashMap<>();
        Gson gson = GsonBuild.buildGson();
        if (rooms != null && !TextUtils.isEmpty(rooms)) {
            map = gson.fromJson(rooms, map.getClass());
        }
        map.put(ccuId, roomIds);
        instance.putString("sortrooms", gson.toJson(map));
    }

    public static List<Integer> getSortFloorIds(String ccuId) {
        SpManager instance = SpManager.getInstance();
        String rooms = instance.getString("sortfloors");
        if (rooms == null || TextUtils.isEmpty(rooms)) {
            return null;
        }
        Map<String, List<Integer>> map = new HashMap<>();
        Gson gson = GsonBuild.buildGson();
        map = gson.fromJson(rooms, map.getClass());
        if (map.keySet().contains(ccuId)) {
            List<Integer> integers = map.get(ccuId);
            if (integers != null && integers.size() > 0) {
                List<Integer> ins = new ArrayList<>();
                for (Object o : integers) {
                    if (o instanceof Number) {
                        int i = ((Double) o).intValue();
                        ins.add(i);
                    }
                }
                return ins;
            }
            return integers;
        }
        return null;
    }

    public static void putSortFloors(String ccuId, List<Integer> roomIds) {
        SpManager instance = SpManager.getInstance();
        String rooms = instance.getString("sortfloors");
        Map<String, List<Integer>> map = new HashMap<>();
        Gson gson = GsonBuild.buildGson();
        if (rooms != null && !TextUtils.isEmpty(rooms)) {
            map = gson.fromJson(rooms, map.getClass());
        }
        map.put(ccuId, roomIds);
        instance.putString("sortfloors", gson.toJson(map));
    }


    public static List<SimpleDevice> getSortSimpleDevices(String ccuId) {
        String devices = SpManager.getInstance().getString("sortdevices");
        if (devices == null || TextUtils.isEmpty(devices)) {
            return null;
        }
        Map<String, List<String>> map = new HashMap<>();
        Gson gson = GsonBuild.buildGson();
        map = gson.fromJson(devices, map.getClass());
        if (map.keySet().contains(ccuId)) {
            List<String> stringSimpleDevices = map.get(ccuId);
            if (stringSimpleDevices != null && stringSimpleDevices.size() > 0) {
                List<SimpleDevice> list = new ArrayList<>();
                for (String s : stringSimpleDevices) {
                    String[] s1 = s.split("--");
                    if (s1 == null || s1.length != 2) {
                        continue;
                    }
                    list.add(new SimpleDevice(s1[0], UniformDeviceType.valueOf(s1[1])));
                }
                return list;
            }
        }
        return null;
    }

    public static void putSortSimpleDevices(String ccuId, List<SimpleDevice> simpleDevices) {
        String devices = SpManager.getInstance().getString("sortdevices");
        Map<String, List<String>> map = new HashMap<>();
        Gson gson = GsonBuild.buildGson();
        if (devices != null && !TextUtils.isEmpty(devices)) {
            map = gson.fromJson(devices, map.getClass());
        }
        List<String> list = null;
        if (simpleDevices != null && simpleDevices.size() > 0) {
            list = new ArrayList<>();
            for (SimpleDevice device : simpleDevices) {
                list.add(device.getDevId() + "--" + device.getDeviceType().name());
            }
        }
        map.put(ccuId, list);
        SpManager.getInstance().putString("sortdevices", gson.toJson(map));
    }


    public static List<Integer> getSorteScenes(String ccuId) {
        SpManager instance = SpManager.getInstance();
        String scenes = instance.getString("sortscenes");
        if (scenes == null || TextUtils.isEmpty(scenes)) {
            return null;
        }
        Map<String, List<Integer>> map = new HashMap<>();
        Gson gson = GsonBuild.buildGson();
        map = gson.fromJson(scenes, map.getClass());
        if (map.keySet().contains(ccuId)) {
            List<Integer> integers = map.get(ccuId);
            if (integers != null && integers.size() > 0) {
                List<Integer> ins = new ArrayList<>();
                for (Object o : integers) {
                    if (o instanceof Number) {
                        int i = ((Double) o).intValue();
                        ins.add(i);
                    }
                }
                return ins;
            }
            return integers;
        }
        return null;
    }

    public static void putSortScenes(String ccuId, List<Integer> scenes) {
        String devices = SpManager.getInstance().getString("sortscenes");
        Map<String, List<Integer>> map = new HashMap<>();
        Gson gson = GsonBuild.buildGson();
        if (devices != null && !TextUtils.isEmpty(devices)) {
            map = gson.fromJson(devices, map.getClass());
        }
        map.put(ccuId, scenes);
        SpManager.getInstance().putString("sortscenes", gson.toJson(map));
    }

    public static String getCameraImg(String ccuId, String cameraId) {
        return SpManager.getInstance().getString(ccuId + "camera" + cameraId);
    }

    public static void putCameraImg(String ccuId, String cameraId, String filepath) {
        SpManager.getInstance().putString(ccuId + "camera" + cameraId, filepath);
    }



}
